package com.logic.modular.game.robot.hero;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.logic.comment.util.XSThreadUtil;
import com.comment.pojo.domain.logic.domain.PlayerHero;
import com.logic.modular.game.player.hero.service.PlayerHeroServiceImpl;
import com.logic.modular.game.player.hero.mapper.PlayerHeroMapper;
import com.logic.modular.gameCore.config.service.impl.CoreConfigServiceImpl;
import com.comment.pojo.domain.logic.core.CoreExperience;
import com.logic.modular.gameCore.cultivate.exp.CoreExperienceServiceImpl;
import com.comment.pojo.domain.logic.core.CoreHero;
import com.comment.pojo.domain.logic.core.CoreHeroStar;
import com.logic.modular.gameCore.cultivate.hero.service.CoreHeroServiceImpl;
import com.logic.modular.gameCore.cultivate.hero.service.CoreHeroStarServiceImpl;
import com.logic.modular.gameCore.cultivate.skill.controller.CoreSkillStarServiceImpl;
import com.comment.pojo.domain.logic.core.CoreSkillStar;
import com.comment.pojo.domain.logic.core.CoreRobotHero;
import com.logic.modular.gameCore.match.robot.service.impl.CoreRobotHeroServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static com.comment.pojo.domain.logic.domain.PlayerHero.recalculateAttributes;

/**
 * 人机英雄行为
 */
@Slf4j
@Service
public class RobotHeroActionServiceImpl {

    @Resource
    private XSThreadUtil xsThreadUtil;
    @Resource
    private PlayerHeroServiceImpl playerHeroService;
    @Resource
    private PlayerHeroMapper playerHeroMapper;
    @Resource
    private CoreSkillStarServiceImpl coreSkillStarServiceImpl;

    /**
     * 初始化机器人-武将(技能)
     */
    public void tryInit() {
        List<CoreRobotHero> robotHeroList = CoreRobotHeroServiceImpl.listCache();
        xsThreadUtil.scheduledExecutorService().schedule(() -> {
            int robotInitValue = CoreConfigServiceImpl.getCache(6L).getValue1().intValue();

            for (long newRobotId = 1; newRobotId <= robotInitValue; newRobotId++) {
                // 获取武将->获取技能->穿戴技能
                for (CoreRobotHero coreRobotHero : robotHeroList) {
                    // 检测是否拥有该武将
                    LambdaQueryWrapper<PlayerHero> check = new LambdaQueryWrapper<>();
                    check.eq(PlayerHero::getUid, newRobotId);
                    check.eq(PlayerHero::getCoreHeroId, coreRobotHero.getCoreHeroId());
                    Long l = playerHeroMapper.selectCount(check);
                    if (l == 0) {
                        obtainHero(newRobotId, coreRobotHero.getCoreHeroId());
                    }
                    PlayerHero playerHero = playerHeroService.select(newRobotId, coreRobotHero.getCoreHeroId());

                    Long skill1 = coreRobotHero.getSkill1();
                    Integer skill1Star = coreRobotHero.getSkill1Star();
                    CoreSkillStar coreSkillStar1 = coreSkillStarServiceImpl.selectBy(skill1, skill1Star);
                    if (coreSkillStar1 != null) {
                        playerHero.setSkillSlot1(coreSkillStar1.getId());
                    }

                    Long skill2 = coreRobotHero.getSkill2();
                    Integer skill2Star = coreRobotHero.getSkill2Star();
                    CoreSkillStar coreSkillStar2 = coreSkillStarServiceImpl.selectBy(skill2, skill2Star);
                    if (coreSkillStar2 != null) {
                        playerHero.setSkillSlot2(coreSkillStar2.getId());
                    }
                    playerHeroService.updateById(playerHero);
                }
            }
        }, 5, TimeUnit.SECONDS);
    }

    public void obtainHero(Long playerId, Long coreHeroId) {
        PlayerHero accountHero = playerHeroService.select(playerId, coreHeroId);
        CoreHero coreHero = CoreHeroServiceImpl.getCache(coreHeroId);
        CoreHeroStar coreHeroStar = CoreHeroStarServiceImpl.getCache(coreHeroId, 0);
        CoreSkillStar coreSkillStar = coreSkillStarServiceImpl.get(coreHero.getSkill());
        if (accountHero == null) {
            accountHero = new PlayerHero();
            accountHero.setUid(playerId);
            accountHero.setCoreHeroId(coreHeroId);
            accountHero.setLevel(1);
            accountHero.setStar(0);
            accountHero.setQuality(coreHero.getQualityLv());
            // 查经验表
            CoreExperience coreExperience = CoreExperienceServiceImpl.getCache(1, 1);
            accountHero.setNextExp(coreExperience.getExp());
            accountHero.setName(coreHero.getName());
            ////////
            accountHero.setSkill(coreSkillStar.getId());
            ////////
            recalculateAttributes(accountHero, coreHero, coreHeroStar);

            playerHeroService.save(accountHero);
        }
    }
}
